package com.anrong.wulansdk;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.anrong.wulansdk.sdk.WLData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity implements OnClickListener{

	private Button btn, btn2, btn_error;
	private Button btn_add, btn_del, btn_modify, btn_query;
	
	private TextView tv_longitude, tv_latitude;
	
	
	String processEventID = "playGame";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		WLData.onAppStart();
		
		btn = (Button) findViewById(R.id.bt);
		btn.setOnClickListener(this);
		btn2 = (Button) findViewById(R.id.bt2);
		btn2.setOnClickListener(this);
		
		btn_error = (Button) findViewById(R.id.bt_error);
		btn_error.setOnClickListener(this);
		btn_add = (Button) findViewById(R.id.add);
		btn_add.setOnClickListener(this);
		btn_del = (Button) findViewById(R.id.del);
		btn_del.setOnClickListener(this);
		btn_modify = (Button) findViewById(R.id.modify);
		btn_modify.setOnClickListener(this);
		findViewById(R.id.g1).setOnClickListener(this);
		findViewById(R.id.g2).setOnClickListener(this);
		findViewById(R.id.g3).setOnClickListener(this);
		
		findViewById(R.id.to).setOnClickListener(this);
		
		findViewById(R.id.close).setOnClickListener(this);
		
		tv_longitude = (TextView) findViewById(R.id.tv_longitude);
		tv_latitude = (TextView) findViewById(R.id.tv_latitude);

	}
	
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt:
			WLData.onModel("登录");
			break;
		case R.id.bt2:
			WLData.onModel("登出");
			break;
		
		case R.id.bt_error:
//			try {
				String[] s = new String[]{"1"};
				String string = s[1];
//			} catch (Exception e) {
//				ByteArrayOutputStream baos = new ByteArrayOutputStream();
//				e.printStackTrace(new PrintStream(baos));
//				String exception = baos.toString();
//				WLData.reportError("admin123", exception);
//			}

			break;
		case R.id.add:
			Map<String, String> map = new HashMap<String, String>();
			map.put("性别", "男");
			map.put("age", "27");
			WLData.onMapping(map);
			break;
		case R.id.del:
			Map<String, String> map2 = new HashMap<String, String>();
			map2.put("性别", "男");
			map2.put("age", "27");
			WLData.onModel("删除", map2);
			break;
		case R.id.modify:
			WLData.onModel("修改");
			break;
		case R.id.g1:
			
			WLData.onProcessEvent(processEventID, "第一关");
			break;
		case R.id.g2:
			WLData.onProcessEvent(processEventID, "第二关");
			break;
		case R.id.g3:
			Map<String, String> map3 = new HashMap<String, String>();
			map3.put("分数", "90");
			WLData.onProcessEvent(processEventID, "第三关", map3);
			break;
			
		case R.id.to:
			startActivity(new Intent(this, TestActivity.class));
			break;
			
		case R.id.close:
			
			finish();
			break;
			
		
			
			
//			openPackage(this, "com.anrongtec.zcpt");
			
//			String externalStorageState = Environment.getExternalStorageState();
//			File[] listFiles = Environment.getRootDirectory().listFiles();
//			File[] listFiles2 = Environment.getDataDirectory().listFiles();
//			File[] listFiles3 = Environment.getExternalStorageDirectory().listFiles();
//			
//			
//			File[] listFiles4 = new File("/storage/66F0-1505/").listFiles();
//			for (File file : listFiles4) {
//				Log.d("123123123", file.getName());
//			}
			
//			File f = new File("/storage/66F0-1505/");
//			boolean canWrite = f.canWrite();
//			boolean canRead = f.canRead();
//			
//			int i = 0;
//			i = i++;
			
		}
	}

	//*******************************测试**********************************
	
	public static Intent getAppOpenIntentByPackageName(Context context, String packageName) {  
        // MainActivity完整名  
        String mainAct = null;  
        // 根据包名寻找MainActivity  
        PackageManager pkgMag = context.getPackageManager();  
        Intent intent = new Intent(Intent.ACTION_MAIN);  
        intent.addCategory(Intent.CATEGORY_LAUNCHER);  
        intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);  
  
        List<ResolveInfo> list = pkgMag.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);  
        for (int i = 0; i < list.size(); i++) {  
            ResolveInfo info = list.get(i);  
            if (info.activityInfo.packageName.equals(packageName)) {  
                mainAct = info.activityInfo.name;  
                break;  
            }  
        }  
        if (TextUtils.isEmpty(mainAct)) {  
            return null;  
        }  
        intent.setComponent(new ComponentName(packageName, mainAct));  
        return intent;  
    }  
  
    public static Context getPackageContext(Context context, String packageName) {  
        Context pkgContext = null;  
        if (context.getPackageName().equals(packageName)) {  
            pkgContext = context;  
        } else {  
            // 创建第三方应用的上下文环境  
            try {  
                pkgContext = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY  
                        | Context.CONTEXT_INCLUDE_CODE);  
            } catch (NameNotFoundException e) {  
                e.printStackTrace();  
            }  
        }  
        return pkgContext;  
    }  
  
    public static boolean openPackage(Context context, String packageName) {  
        Context pkgContext = getPackageContext(context, packageName);  
        Intent intent = getAppOpenIntentByPackageName(context, packageName);  
        if (pkgContext != null && intent != null) {  
            pkgContext.startActivity(intent);  
            return true;  
        }  
        return false;  
    }
	
    
    
   //**********************************************************************************************************
    
    
    @Override
    protected void onStart() {
    	WLData.onPageStart("main");
    	super.onStart();
    }
    @Override
    protected void onStop() {
    	WLData.onPageStop("main");
    	super.onStop();
    }
    
    @Override
    public void onBackPressed() {
    	super.onBackPressed();
    	finish();
    }
	@Override
	protected void onDestroy() {
		WLData.onAppEnd();
		super.onDestroy();
	}
    
    
}
